//
//  IslandsController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/5/12.
//  Copyright © 2016年 game. All rights reserved.
//

#import "IslandsController.h"
#import "SelectNavView.h"
#import "IslanDescribCtll.h"
#import "IslanSearchController.h"
#import "MainPageController.h"
#import "DiaochangController.h"
#import "IslanSegView.h"
#import "IslanLocalView.h"
#import "Masonry.h"
@interface IslandsController ()<IslanSegViewDeleget>

@property (nonatomic,strong) NSArray *pageArray;
@property (nonatomic,strong) AppDelegate *app;
@property (nonatomic,strong) NSUserDefaults *Myud;
@property (nonatomic,strong) IslanLocalView *local;
@property (nonatomic,strong) IslanSegView *seg;
@end

@implementation IslandsController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.app.myTabBar.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden=YES;
    self.navigationController.navigationBarHidden = YES;
    StatusBlack;
}

-(void)setThoroughfare:(NSString *)thoroughfare{
    _thoroughfare = thoroughfare;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.local.localShow.text = _thoroughfare;
    });
}

-(void)setIsYuJuDian:(BOOL)isYuJuDian{
    _isYuJuDian = isYuJuDian;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.seg.segment.selectedSegmentIndex = 1*_isYuJuDian;
        
        [self setViewControllers:@[_pageArray[_isYuJuDian]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
        UIViewController *ctr = _pageArray[_isYuJuDian];
        [self.view sendSubviewToBack:ctr.view];

    });
    
}

- (NSArray *)pageControllers {
    DiaochangController *diaochang = [DiaochangController new];
    diaochang.dic = @{@"city":[self.Myud valueForKey:@"cityPinY"]};
    diaochang.url = IslanUrl;
    diaochang.CharactModel = self.CharactModel;
    diaochang.isYujudian = 0;
    
    
    DiaochangController *dianpu = [DiaochangController new];
    
    dianpu.dic = @{@"city":[self.Myud valueForKey:@"cityPinY"]};
    dianpu.url = DianpuUrl;
    dianpu.isYujudian = 1;
    dianpu.CharactModel = self.CharactModel;
    return @[diaochang, dianpu];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.frame =  [UIScreen mainScreen].bounds;

  self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    self.Myud = [NSUserDefaults standardUserDefaults];
    
    _pageArray = [self pageControllers];
    [self setViewControllers:@[_pageArray[0]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    
    _seg = [[NSBundle mainBundle]loadNibNamed:@"IslanSegView" owner:nil options:nil].lastObject;
    _seg.deleget = self;
    [self.view addSubview:_seg];
    [_seg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.equalTo(@64);
  
    }];
    
    _local = [[NSBundle mainBundle]loadNibNamed:@"IslanLocalView" owner:nil options:nil].lastObject;
    __weak typeof(self) WeakSelf = self;
    _local.LocalBlock = ^{
        [WeakSelf LocalAginShow];
    };
    [self.view addSubview:_local];
    
    [_local mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.equalTo(@30);
        make.bottom.equalTo(@-46);
    }];
}
-(void)LocalAginShow{

//    if( [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {//没有打开定位服务
//        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您尚未开启定位，是否开启" preferredStyle:UIAlertControllerStyleAlert];
//        
//        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
//            
//        }];
//        
//        UIAlertAction *OKcancel = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            
//            NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
//            if([[UIApplication sharedApplication] canOpenURL:url]) {
//                NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString];
//                [[UIApplication sharedApplication] openURL:url];
//            }
//            
//        }];
//
//        
//        [alertController addAction:cancel];
//        
//          [alertController addAction:OKcancel];
//        
//        [self presentViewController:alertController animated:YES completion:nil];
//
//        
//    }else{//打开了
//        
//        _local.localShow.text = @"定位中...";
//        UINavigationController *mainPage = self.tabBarController.viewControllers[0];
//        MainPageController *main = (MainPageController *)mainPage.topViewController;
//        [main locate];
//    }

}

#pragma mark--IslandsControllerView--Delget
-(void)GoBack{
    
    self.app.myTabBar.selectedIndex = 0;
}
#pragma mark--点击搜索
-(void)SeachWhithIndex:(NSInteger)index{
    
    IslanSearchController *seach = [IslanSearchController new];
    seach.isYuJuDian = index;
    seach.CharactModel = self.CharactModel;
    [self.navigationController pushViewController:seach animated:YES];
}

#pragma mark--点击钓场 渔具店选项
-(void)segmentActionWhithIndex:(NSInteger)index{

    [self setViewControllers:@[_pageArray[index]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    UIViewController *ctr = _pageArray[index];
    [self.view sendSubviewToBack:ctr.view];

}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}

@end
